package com.knightboy.smartshoes.outerNavigation;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by knightBoy on 2015/7/30.
 */
public class OuterLocationService extends Service {
    //定位客户端声明
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();

    //自身位置的获得
    private BDLocation mBdLocation;

    //目的地字符串
    private String destinationStr = "";
    //用户真实的前进方向
    private float yourDirection;

    //一个定时器和一个定时任务
    private Timer mTimer;
    private TimerTask mTimerTask;

    //传感器对象
    private SensorManager sensorManager;
    private Sensor sensor;

    //更新主界面的handler类
    private static Handler mHandler;

    public static void setmHandler(Handler mHandler) {
        OuterLocationService.mHandler = mHandler;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //获得传感器服务
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
//        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);

        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener( myListener );            //注册监听函数
        initLocation();                                                    //初始化位置，获取到位置

        //开始反复导航
        //初始化一个定时器和一个定时任务
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                //获取到访问百度后的返回结果
                String urlString = MakeUrl.makeAUrl(mBdLocation);
                String resultString = RequestByGet.getInfomation(urlString);
                //获得正在行走的方向，以及解析百度返回的json数据
                //得到方向值，并更新UI
                yourDirection = mBdLocation.getDirection();
                int BDdirection = ResolvePath.getAPath(resultString);
                int direction = ResolvePath.getADirection(BDdirection, yourDirection);
                //获得转向信息后发给主界面
                Message message = new Message();
                message.arg1 = 1;
                message.arg2 = direction;
                mHandler.sendMessage(message);
            }
        };
        //绑定定时器跟定时任务
        mTimer.schedule(mTimerTask, 1000, 3000);

        return super.onStartCommand(intent, flags, startId);
    }

    private void initLocation(){
        //设置参数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("bd09ll");                            //返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(3000);                                //设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true);                           //返回的定位结果包含地址信息
        option.setAddrType("all");                               //值为 all时，表示返回地址信息，其他值都表示不返回地址信息
        option.setNeedDeviceDirect(true);                        //返回的定位结果包含手机机头的方向
        option.setOpenGps(true);                                 //设置打开GPS
        option.setProdName("KB");
        mLocationClient.setLocOption(option);
        //注册监听函数
        mLocationClient.registerLocationListener( myListener );
        //启动定位SDK
        mLocationClient.start();
        //发起定位，异步获取当前结果
        mLocationClient.requestLocation();
    }

    //得到自身位置监听器类
    class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            mBdLocation = bdLocation;
        }
    }
}


